"""
transcript_window.py

This file is part of a program that implements a Software-Defined Radio.

Copyright (C) 2014 by G3UKB Bob Cowdery

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The authors can be reached by email at:

	bob@bobcowdery.plus.com

"""

# Python imports
import sys, os
import logging
import datetime
from time import sleep
from PyQt4 import QtCore, QtGui

# Application imports
from common.defs import *
from common import persist
from transcript.transcript_common import *

"""
The window prints messages that the system thinks the user should see stratight away.
"""
class TranscriptWindow(QtGui.QMainWindow):
	
	MAX_MESSAGES = 100
	
	def __init__(self):
		
		super(TranscriptWindow, self).__init__()
		
		# State path
		self.STATE_FILE = 'transcript.state'
		
		self.__state = persist.getSavedState(self.STATE_FILE)
		if self.__state == None:
			# Apply defaults
			self.__state = DEFAULT_STATE
			persist.saveState(self.STATE_FILE, self.__state)

		# Set the back colour
		palette = QtGui.QPalette()
		palette.setColor(QtGui.QPalette.Background,QtGui.QColor(43,63,68,255))
		self.setPalette(palette)
		
		# Window settings
		self.setToolTip('SdrScript Control Interface')
		self.statusBar().showMessage('No messages')
		QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
		self.resize(self.__state[WINDOW_WIDTH], self.__state[WINDOW_HEIGHT])
		self.move(self.__state[WINDOW_X], self.__state[WINDOW_Y])
		self.setWindowTitle('SdrScript Transcript')
		
		# Put a list box in the window, it's all we need
		self.__message_list = QtGui.QListWidget()
		self.setCentralWidget(self.__message_list)
		
		self.show()
		self.repaint()
	
	def close(self):
		""" Terminate the window """
		
		# Update window position
		position = self.pos()
		self.__state[WINDOW_X] = position.x()
		self.__state[WINDOW_Y] = position.y()
		self.__state[WINDOW_WIDTH] = self.width()
		self.__state[WINDOW_HEIGHT] = self.height()
		# Save to the state file
		persist.saveState(self.STATE_FILE, self.__state)
		# Quit the window
		QtCore.QCoreApplication.instance().quit()
		
	def add_message(self, severity, source, msg):
		"""
		Add a message to the message list and if necessary cull the list
		
		Arguments:
			severity	-- INFO etc
			source		-- message source
			msg			-- the text message to add
		
		"""
		
		t = datetime.datetime.now().strftime('%H:%M:%S')
		if severity == logging.INFO: severity = 'INFO'
		elif severity == logging.WARN: severity = 'WARN'
		elif severity == logging.ERROR: severity = 'ERROR'
		elif severity == logging.CRITICAL: severity = 'CRITICAL'
		else: severity = 'INFO'
		
		text = '%s - %s - %s - %s' % (t, severity, source, msg)
		self.__message_list.addItem(text)
		
		if self.__message_list.count() > MAX_MESSAGES:
			# Remove the oldest item
			self.__message_list.take(0)
		
		# Make sure last item is visible.
		# Unfortunately this does not work
		self.__message_list.scrollToBottom()
			